package com.cloud.zookeeper.server.client.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by Even on 2016/10/14.
 */
@RestController
public class ZookeeperClient {
    @Autowired
    private LoadBalancerClient loadBalancer;
    @Autowired
    private DiscoveryClient discovery; //发现服务
    //@Value("${spring.application.name:}")
    private String serverName;


    @RequestMapping("/")
    public ServiceInstance getChooseServerName() {
        return this.loadBalancer.choose("spring-cloud-zookeeper-server");
    }
    @RequestMapping("/getLocalService")
    public String getLocalService() {
        return "Hello World! from " + this.discovery.getLocalServiceInstance();
    }
    @RequestMapping("/getServic")
    public String getServic(){
        List<ServiceInstance> list = discovery.getInstances("spring-cloud-zookeeper-server");
        if (list != null && list.size() > 0 ) {
            return list.get(0).getUri().toString();
        }
        return null;
    }

}
